package com.ahpome.comm;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.ahpome.util.FormatTool;

public class FileUpload {
	/**
	 * 图片上传
	 * @param request
	 * @param path
	 * @return
	 */
	public static List<String> phtotPath(HttpServletRequest request,String path){
		List paths=new ArrayList<String>();
		try {
			// 创建一个通用的多部分解析器
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
					request.getSession().getServletContext());
			// 判断 request 是否有文件上传,即多部分请求
			if (multipartResolver.isMultipart(request)) {
				// 转换成多部分request
				MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				MultipartHttpServletRequest multiRequest = resolver.resolveMultipart(request);
				// 取得request中的所有文件名
				Iterator<String> iter = multiRequest.getFileNames();
				while (iter.hasNext()) {
					// 取得上传文件
					MultipartFile file = multiRequest.getFile(iter.next());
					if (file != null) {
						// 取得当前上传文件的文件名称
						String myFileName = file.getOriginalFilename();
						// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
						if (myFileName.trim() != "") {
							String ext = getExt(myFileName);// 获取后缀名
							/*if (!filterFiles(ext)) {
								return "";
							}	*/	
							// 重命名上传后的文件名
							String fileName =FormatTool.fileName(ext);
							// 获取文件大小
							long size = file.getSize();
							// 定义上传路径
							String filePath= request.getSession().getServletContext().getRealPath("/")+path;
							File isfile = new File(filePath);
							if (!isfile.exists() && !isfile.isDirectory()) {
								boolean isOk = isfile.mkdirs();
							}
							String filePath2 = filePath +"\\"+ fileName;
							paths.add(path+"/"+fileName);
							File localFile = new File(filePath2);
							file.transferTo(localFile);
						}
					}
				}
			} else {
				return null;
			}
		} catch (Exception ex) {
			System.out.println(ex);
		}
		return paths;
	}
	public static String phtotPathForString(HttpServletRequest request,String path){
		String paths="";
		try {
			// 创建一个通用的多部分解析器
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
					request.getSession().getServletContext());
			// 判断 request 是否有文件上传,即多部分请求
			if (multipartResolver.isMultipart(request)) {
				// 转换成多部分request
				MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				MultipartHttpServletRequest multiRequest = resolver.resolveMultipart(request);
				// 取得request中的所有文件名
				Iterator<String> iter = multiRequest.getFileNames();
				while (iter.hasNext()) {
					// 取得上传文件
					MultipartFile file = multiRequest.getFile(iter.next());
					if (file != null) {
						// 取得当前上传文件的文件名称
						String myFileName = file.getOriginalFilename();
						// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
						if (myFileName.trim() != "") {
							String ext = getExt(myFileName);// 获取后缀名
							/*if (!filterFiles(ext)) {
								return "";
							}	*/	
							// 重命名上传后的文件名
							String fileName =FormatTool.fileName(ext);
							// 获取文件大小
							long size = file.getSize();
							// 定义上传路径
							String filePath= request.getSession().getServletContext().getRealPath("/")+path;
							File isfile = new File(filePath);
							if (!isfile.exists() && !isfile.isDirectory()) {
								boolean isOk = isfile.mkdirs();
							}
							String filePath2 = filePath +"\\"+ fileName;
							paths=path+"/"+fileName;
							File localFile = new File(filePath2);
							file.transferTo(localFile);
						}
					}
				}
			} else {
				return "";
			}
		} catch (Exception ex) {
			System.out.println(ex);
		}
		return paths;
	}

	public static boolean filterFiles(String fileName) {
		String reg = "(JPEG|jpeg|JPG|jpg|GIF|gif|BMP|bmp|PNG|png)$";
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(fileName.toLowerCase());
		return matcher.find();
	}
	/**
	 * 获取文件名的后缀
	 */
	private static String getExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
	}

	/**
	 * 获取文件名
	 * 
	 * @param fileName
	 * @return
	 */
	private static String getFileName(String fileName) {
		String[] str = fileName.split("\\.");
		return str[0];
	}
}
